//
//  THImageToolBase.m
//  THImageEditDemo
//
//  Created by 希达 on 2018/3/27.
//  Copyright © 2018年 Tan.huang. All rights reserved.
//

#import "THImageTool.h"
#import "THBaseButton.h"
#import "UIView+Layout.h"

@interface THImageTool()

@property (weak, nonatomic) UIButton  *btn;

@end


@implementation THImageTool

- (void)dealloc {
    NSLog(@"%@释放了", NSStringFromClass(self.class));
}

- (void)configView {
    CGFloat count = self.imageArray.count;
    BOOL isCount = self.imageSelectArr.count == 0 ? false : true;
    for (int i = 0; i < count; i ++) {
        NSString *imageName = self.imageArray[i];
        THBaseButton *btn = [THBaseButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = [UIFont systemFontOfSize:14.5];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:isCount ? self.imageSelectArr[i] : imageName] forState:UIControlStateSelected];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [btn setTitle:self.titleArray[i] forState:UIControlStateNormal];
        btn.type = 1;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];

        if (self.isSeparate && i < count - 1) {
            UIView *view = [[UIView alloc] init];
            view.backgroundColor = [UIColor darkTextColor];
            [self addSubview:view];
        }
    }
}


- (void)btnClick:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"Invert cutout"]) {
        sender.selected = !sender.selected;
    } else {
        if (self.isSelect) {
            sender.selected = !sender.selected;
        } else {
            if (self.btn != sender) {
                self.btn.selected = NO;
                sender.selected = YES;
                self.btn = sender;
            }else{
                self.btn.selected = YES;
            }
        }
    }
    if (self.btnClickBlock) {
        self.btnClickBlock(sender.titleLabel.text, sender.isSelected);
    }
}

- (void)setBtnWithStatus:(BOOL)isBold isItalic:(BOOL)isItalic {
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[THBaseButton class]]) {
            THBaseButton *btn = (THBaseButton *)view;
            if ([btn.titleLabel.text isEqualToString:@"Bold"]) {
                btn.selected = isBold;
            } else if ([btn.titleLabel.text isEqualToString:@"Italic"]) {
                btn.selected = isItalic;
            }
        }
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat count = self.titleArray.count == 0 ? self.imageArray.count : self.titleArray.count;
    CGFloat btnWidth = 85;
    CGFloat btnHeight = self.tz_height < 60 ? self.tz_height : 60;
    CGFloat top = self.tz_height * 0.5 - btnHeight * 0.5;
    CGFloat padding = (self.tz_width - btnWidth * count) / (count + 1);

    CGFloat index = 0;
    for (int i = 0; i < self.subviews.count; i ++) {
        UIView *view = self.subviews[i];
        if ([view isKindOfClass:[THBaseButton class]]) {
            THBaseButton *btn = (THBaseButton *)view;
            btn.frame = CGRectMake(padding + index * (btnWidth + padding), top, btnWidth, btnHeight);
            index += 1;
        } else {
            view.frame = CGRectMake(padding * 0.5 + index * (btnWidth + padding), top, 1, btnHeight);
        }
    }

}



@end
